/*--------------------------------------------------------------------------------------*\
**
** adv_border_gate.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_border_gate.h"

#include "adaptor_handler.h"
#include "adv_keymaster_tent.h"
#include "adv_object_model.h"
#include "adv_object_strings.h"
#include "adv_object_type.h"
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "creature.h"
#include "creature_type.h"
#include "hero.h"
#include "object_registration.h"
#include "music.h"
#include "player.h"
#include "replace_keywords.h"
#include "simple_dialog.h"

static t_object_registration<t_adv_border_gate> k_registration( k_adv_object_border_gate );

/*
** Adventure border gate constructor.
*/
t_adv_border_gate::t_adv_border_gate ( std::string const& source )
         : t_stationary_adventure_object( source )
{
}

void t_adv_border_gate::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                               t_direction direction, t_adventure_frame* frame )
{
    t_player const* player = army->get_owner();
	int				key_type = get_major_subtype();

    /*
    ** Check if player has key.
    */
    if (!player->has_key( key_type ) && player && !player->is_computer())
    {

	    std::string					  denied_text;
	    std::string					  object_text;
        t_counted_ptr<t_basic_dialog> denied_dialog_ptr;
        t_qualified_adv_object_type   adv_object_type( k_adv_object_keymaster_tent, 
													   key_type );
        
        object_text = ::get_name ( adv_object_type );
    	denied_text = replace_keywords( get_text( "Denied" ), "%keymaster_tent_name",
										object_text );
        denied_dialog_ptr = new t_basic_dialog;
        denied_dialog_ptr->set_text( denied_text, k_justify_left );

        denied_dialog_ptr->add_ok_button();
        denied_dialog_ptr->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_quest )->play( get_sound_volume() );
        denied_dialog_ptr->run_modal();
		play_music( music );
    }
}

// ---------------------------------------------------------------------------
// check if army has permission to pass through gate
// ---------------------------------------------------------------------------
bool t_adv_border_gate::blocks_army( t_creature_array const& army, t_path_search_type path_type ) const
{
	t_player const* player = army.get_owner();

	if (player == 0)
		return true;
	return !player->has_key( get_major_subtype() );
}

// ---------------------------------------------------------------------------
// check if army has permission to pass through gate
// ---------------------------------------------------------------------------
bool t_adv_border_gate::is_triggered_by( t_creature_array const& army ) const
{
	t_player const* player = army.get_owner();

	if (player == 0)
		return true;
	return !player->has_key( get_major_subtype() );
}

// -------------------------------------------------------------
// For compatibility.
// -------------------------------------------------------------
bool t_adv_border_gate::write( std::streambuf& stream ) const
{
	put<int>( stream, 0 );
	return true;
}

// -------------------------------------------------------------
// For compatibility.
// -------------------------------------------------------------
bool t_adv_border_gate::read( std::streambuf&						stream, 
							  t_qualified_adv_object_type const&	type,
							  int									version )

{
	int single_use_id = get<int>( stream );
	return true;
}

